package cn.xiaolang.function.java_gc;

import cn.xiaolang.common.utils.Monitor;

import java.util.concurrent.TimeUnit;

/**
 * java版本jdk1.8.0_201
 * <p>
 * 空间分配 eden=40m, s0=5m, s1=5m, old=50m. total=95m;
 * </p>
 *
 * <ui>CMS启动参数：</ui>
 * <p>
 * 简洁：-Xms100m -Xmx100m -Xmn50m -XX:SurvivorRatio=8 -XX:MaxTenuringThreshold=10 -XX:+PrintGCDetails -XX:+PrintGCDateStamps
 * 详细：-Xms100m -Xmx100m -Xmn50m -XX:SurvivorRatio=8 -XX:MaxTenuringThreshold=10 -XX:+UseConcMarkSweepGC -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCDateStamps  -XX:+PrintHeapAtGC -Xloggc:/tmp/java_gc-demo/cms-java_gc.log
 * </p>
 *
 * <ui>G1启动参数:</ui>
 * <p>
 * -Xms100m -Xmx100m -XX:+UseG1GC -XX:+PrintGC -XX:+PrintGCDetails -XX:+PrintGCDateStamps  -XX:+PrintHeapAtGC -Xloggc:/tmp/java_gc-demo/g1-java_gc.log
 * </p>
 */
public class CmsLogDemo {

    private static final int MB = 1024 * 1024;

    public static void main(String[] args) throws InterruptedException {

        // waiting open the VisualVM
        TimeUnit.SECONDS.sleep(15);

        Monitor.printlnMemory("1.初始化Xmx100M(eden=40m, s0/s1=5m, old=50m)");
        byte[] allocation = new byte[32 * MB];
        Monitor.printlnMemory("2.创建32M对象");
        Monitor.printlnMemory("3.32M对象<eden区大不触发GC");

        byte[][] additionalObjTip = new byte[10][];
        TimeUnit.SECONDS.sleep(3);
        Monitor.printlnMemory("4.创建一个10大小的数组");

        /*
         * 2*3M > s0区
         */
        for (int i = 0; i < 10; ++i) {
            additionalObjTip[i] = new byte[3 * MB];
            Monitor.printlnMemory("第[" + (i+1) + "]次 创建一个3MB对象");
            TimeUnit.SECONDS.sleep(2);
        }
        System.out.println(additionalObjTip.length);
        TimeUnit.SECONDS.sleep(5);
    }


}